package com.sgb.kjwl.viewmodel.self.collect;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.SelfPickListEntity3;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.event.CollegeEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.fragment.self.pick.SelfPickListFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfPickListFragModel3 extends BaseViewModel {
    public ObservableField<String> areaCode = new ObservableField<>("");
    public ObservableField<String> type = new ObservableField<>("");
    public ObservableField<String> status = new ObservableField<>("");
    private SelfPickListFragment3 fragment3;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public SelfPickListFragModel3(SelfPickListFragment3 fragment3, SelfRepertory repertory) {
        this.fragment3 = fragment3;
        this.repertory = repertory;
    }

    //工程招采列表
    public void getList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        repertory.getCollectList(page,size,status.get(), type.get(),"","")
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SelfPickListEntity3>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SelfPickListEntity3>>> listResponseBaseEntity) throws Exception {
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment3.showList(listResponseBaseEntity.getData().getList(),isLoadMore);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void delList(String no) {
        repertory.deleteCollectList(no)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            EventBus.getDefault().post(CollegeEvent.make().setMessageTag(EventConstant.EVENT_COLLECT_SELF_LIST));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void delList2(String no) {
        repertory.deleteCollectList2(no)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            EventBus.getDefault().post(CollegeEvent.make().setMessageTag(EventConstant.EVENT_COLLECT_SELF_LIST));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getH5Url(String id,int status){
        DialogHelper.showProgressDialog(fragment3.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("detail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type="+status;
                    fragment3.showUrl(url);
                } else {
                    MToast.showToast(fragment3.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
